package org.cloudbus.cloudsim.examples.power.parallel.helper;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

public class DistributeJobChart extends ApplicationFrame {

	private String xAxis = "";
	private String titleChart = "";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<Integer> list;

	public DistributeJobChart(String title, List<Integer> infos,String xAis, String titleChart) {
		super(title);
		// TODO Auto-generated constructor stub
		this.list = infos;
		this.xAxis = xAis;
		this.titleChart = titleChart;
		CategoryDataset dataset = createDataset();
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(900,500));
		setContentPane(chartPanel);		
	}

	private CategoryDataset createDataset() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (int i = 0; i < list.size(); i++) {
			dataset.addValue(list.get(i), "Classes", String.valueOf(i + 1));
		}
		return dataset;
	}

	@SuppressWarnings("deprecation")
	private JFreeChart createChart(CategoryDataset dataset) {
		// create the chart...
		JFreeChart chart = ChartFactory.createLineChart(
				"The Job Distribution Based on Submit Time ", // chart title
				xAxis, // domain axis label
				"Number Of Job", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				false, // include legend
				true, // tooltips
				false // urls
				);
		chart.addSubtitle(new TextTitle(titleChart));
		TextTitle source = new TextTitle(
				"Source: written by Nguyen Hoai Nam");
	   // source.setFont(new Font("SansSerif", Font.PLAIN, 10));
		source.setPosition(RectangleEdge.BOTTOM);
		source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		chart.addSubtitle(source);
		chart.setBackgroundPaint(Color.white);
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.black);

		// customise the range axis...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// customise the renderer...
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.white);
		renderer.setPaint(Color.red);		
		return chart;
	}

	/**
	 * Creates a panel for the demo (used by SuperDemo.java).
	 * 
	 * @return A panel.
	 */
	public JPanel createDemoPanel() {
		JFreeChart chart = createChart(createDataset());		
		return new ChartPanel(chart);
	}

	@Override
	public void windowClosing(WindowEvent event) {
		// TODO Auto-generated method stub		
	}
	
	

}
